package cn.burnit.view;

import cn.burnit.pojo.Book;
import cn.burnit.service.BookService;
import org.jdesktop.swingx.JXDatePicker;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.Date;

/**
 * 更新书籍
 */
public class UpdateBookForm extends JFrame {

    private JLabel checkBookId,bookId,bookName,bookType,translator,publishTime,stock,price,publisher,author;
    private JTextField checkBookIdField,bookIdField,bookNameField,translatorField,stockField,priceField,publisherField,authorField;
    private JButton btn_Update,btn_Cancel,btn_Check;
    private JComboBox<String> comboBox;
    private  BookService bookService=new BookService();
    final JXDatePicker datepick = new JXDatePicker();

    public UpdateBookForm(){
        Container container = getContentPane();
        container.setLayout(null);

        btn_Update=new JButton("保存");
        btn_Update.setBounds(190,310,80,20);
        btn_Cancel=new JButton("取消");
        btn_Cancel.setBounds(320,310,80,20);
        btn_Check=new JButton("查询");
        btn_Check.setBounds(360,10,80,20);
        btn_Cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        //查询监听事件
        btn_Check.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                checkBookById(e);
            }
        });
        //修改按钮监听事件
        btn_Update.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    updateBook(e);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        checkBookId=new JLabel("图书编号");
        checkBookId.setFont(new Font("宋体", Font.PLAIN, 16));
        checkBookId.setBounds(120,10,100,20);

        bookId=new JLabel("图书编号");
        bookId.setFont(new Font("宋体", Font.PLAIN, 16));
        bookId.setBounds(50,50,100,20);
        bookType=new JLabel("图书类型");
        bookType.setFont(new Font("宋体", Font.PLAIN, 16));
        bookType.setBounds(50,90,100,20);
        translator=new JLabel("译者");
        translator.setFont(new Font("宋体", Font.PLAIN, 16));
        translator.setBounds(50,130,100,20);
        publishTime=new JLabel("出版时间");
        publishTime.setFont(new Font("宋体", Font.PLAIN, 16));
        publishTime.setBounds(50,170,100,20);
        stock=new JLabel("库存数量");
        stock.setFont(new Font("宋体", Font.PLAIN, 16));
        stock.setBounds(50,210,100,20);
        bookName=new JLabel("图书名称");
        bookName.setFont(new Font("宋体", Font.PLAIN, 16));
        bookName.setBounds(280,50,100,20);
        author=new JLabel("作者");
        author.setFont(new Font("宋体", Font.PLAIN, 16));
        author.setBounds(280,90,100,20);
        publisher=new JLabel("出版社");
        publisher.setFont(new Font("宋体", Font.PLAIN, 16));
        publisher.setBounds(280,130,100,20);
        price=new JLabel("定价");
        price.setFont(new Font("宋体", Font.PLAIN, 16));
        price.setBounds(280,170,100,20);

        checkBookIdField=new JTextField();
        checkBookIdField.setColumns(10);
        checkBookIdField.setBounds(200,10,130,20);

        bookIdField=new JTextField();
        bookIdField.setColumns(10);
        bookIdField.setBounds(120,50,130,20);
        String[] ty=new String[]{"文学","理学"};
        comboBox = new JComboBox<>(ty);
        comboBox.setBounds(120,90,130,20);
        translatorField=new JTextField();
        translatorField.setColumns(10);
        translatorField.setBounds(120,130,130,20);
        Date date = new Date();
        // 设置 date日期
        datepick.setDate(date);
        datepick.setBounds(120,170,130,20);

        stockField=new JTextField();
        stockField.setColumns(10);
        stockField.setBounds(120,210,130,20);
        bookNameField=new JTextField();
        bookNameField.setColumns(10);
        bookNameField.setBounds(360,50,130,20);
        authorField=new JTextField();
        authorField.setColumns(10);
        authorField.setBounds(360,90,130,20);
        publisherField=new JTextField();
        publisherField.setColumns(10);
        publisherField.setBounds(360,130,130,20);
        priceField=new JTextField();
        priceField.setColumns(10);
        priceField.setBounds(360,170,130,20);

        container.add(checkBookId);container.add(checkBookIdField);container.add(btn_Check);
        container.add(bookId);container.add(bookIdField);
        container.add(bookName);container.add(bookNameField);
        container.add(bookType);container.add(comboBox);
        container.add(author);container.add(authorField);
        container.add(translator);container.add(translatorField);
        container.add(publisher);container.add(publisherField);
        container.add(publishTime);container.add(datepick);
        container.add(price);container.add(priceField);
        container.add(stock);container.add(stockField);
        container.add(btn_Update);container.add(btn_Cancel);


        setTitle("修改图书");
        setSize(600,400);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    /**
     * 更新图书
     * @param e
     * @throws ParseException
     */
    private void updateBook(ActionEvent e) throws ParseException {
            String booId = this.bookIdField.getText();
            String bookName = this.bookNameField.getText();
            String bookType = (String) this.comboBox.getSelectedItem();
            String author = this.authorField.getText();
            String translator = this.translatorField.getText();
            String publisher = this.publisherField.getText();
            Date date = this.datepick.getDate();
            java.sql.Date publishTime = new java.sql.Date(date.getTime());
            float price = Float.parseFloat(this.priceField.getText());
            int stock = Integer.parseInt(this.stockField.getText());
            Book book = new Book(booId, bookName, bookType, author, translator, publisher, publishTime, price, stock);
            int i=bookService.updateBook(book);
            if (i>0){
                JOptionPane.showMessageDialog(null,"修改成功");
                dispose();
            }else {
                JOptionPane.showMessageDialog(null,"修改失败");
            }

    }

    /**
     * 通过id查找Book
     * @param e
     */
    private void checkBookById(ActionEvent e){
        String id = this.checkBookIdField.getText();

        Book book=bookService.getBookById(id);
        //System.out.println(book);
        //图书不为空，设置文本框
        if (book!=null){
            bookIdField.setText(book.getId());
            bookNameField.setText(book.getBookname());
            comboBox.setSelectedItem(book.getBooktype());
            authorField.setText(book.getAuthor());
            translatorField.setText(book.getTranslator());
            publisherField.setText(book.getPublisher());
            datepick.setDate(book.getPublish_time());
            priceField.setText(String.valueOf(book.getPricce()));
            stockField.setText(String.valueOf(book.getStock()));
        }else {
            JOptionPane.showMessageDialog(null,"内容为空");
        }
    }

//    public static void main(String[] args) {
//        new UpdateBookForm();
//    }
}
